package bomberman.img;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import javax.swing.ImageIcon;

/**
 *
 * @author eirikb
 */
public class ImageDB implements Serializable {
    
    private long version;

    private Hashtable<String, ImgCharacter> character;
    private Hashtable<String, ImgFire> fire;
    private Hashtable<String, ArrayList<ImageIcon>> bomb;
    private Hashtable<String, ArrayList<ImageIcon>> brick;
    private Hashtable<String, ImageIcon> box;
    private Hashtable<String, ImageIcon> powerup;
    private ImageIcon welcome;

    public ImageDB() {
        character = new Hashtable<String, ImgCharacter>();
        fire = new Hashtable<String, ImgFire>();
        bomb = new Hashtable<String, ArrayList<ImageIcon>>();
        brick = new Hashtable<String, ArrayList<ImageIcon>>();
        box = new Hashtable<String, ImageIcon>();
        powerup = new Hashtable<String, ImageIcon>();
        version = 1;
    }

    public void setBomb(Hashtable<String, ArrayList<ImageIcon>> bomb) {
        this.bomb = bomb;
    }

    public void setBox(Hashtable<String, ImageIcon> box) {
        this.box = box;
    }

    public void setBrick(Hashtable<String, ArrayList<ImageIcon>> brick) {
        this.brick = brick;
    }

    public void addCharacter(String name, ImgCharacter character) {
        this.character.put(name, character);
    }

    public void addFire(String name, ImgFire fire) {
        this.fire.put(name, fire);
    }

    public void setPowerup(Hashtable<String, ImageIcon> powerup) {
        this.powerup = powerup;
    }

    public void setWelcome(ImageIcon welcome) {
        this.welcome = welcome;
    }

    public Hashtable<String, ArrayList<ImageIcon>> getBomb() {
        return bomb;
    }

    public Hashtable<String, ImageIcon> getBox() {
        return box;
    }

    public Hashtable<String, ArrayList<ImageIcon>> getBrick() {
        return brick;
    }

    public Hashtable<String, ImgCharacter> getCharacter() {
        return character;
    }

    public Hashtable<String, ImgFire> getFire() {
        return fire;
    }

    public Hashtable<String, ImageIcon> getPowerup() {
        return powerup;
    }

    public ImageIcon getWelcome() {
        return welcome;
    }
    
    public void generateImages() {
        Iterator<ImgCharacter> cAll = character.values().iterator();
        while (cAll.hasNext()) {
            ImgCharacter c = cAll.next();
            for (int i = 0; i < c.getLeft().size(); i++) {
                c.addRight(flipV(c.getLeft().get(i)));
            }
            int size = c.getUp().size();
            for (int i = 1; i < size; i++) {
                c.addUp(flipV(c.getUp().get(i)));
            }
            size = c.getDown().size();
            for (int i = 1; i < size; i++) {
                c.addDown(flipV(c.getDown().get(i)));
            }
        }
        Iterator<ImgFire> fAll = fire.values().iterator();
        while (fAll.hasNext()) {
            ImgFire f = fAll.next();
            for (int i = 0; i < f.getLeft().size(); i++) {
                f.addUp(rotate(f.getLeft().get(i)));
                f.addRight(rotate(f.getUp().get(i)));
                f.addDown(rotate(f.getRight().get(i)));
            }
            for (int i = 0; i < f.getSide1().size(); i++) {
                f.addSide2(rotate(f.getSide1().get(i)));
            }
        }
    }

    private ImageIcon flipV(ImageIcon image) {
        Image img = image.getImage();
        BufferedImage b = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        b.getGraphics().drawImage(img,
                img.getWidth(null), 0, 0, img.getHeight(null),
                0, 0, img.getWidth(null), img.getHeight(null),
                null);
        return new ImageIcon(b);
    }

    private ImageIcon rotate(ImageIcon image) {
        Image img = image.getImage();
        BufferedImage b = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
        double x = 0.5 * b.getWidth();
        double y = 0.5 * b.getHeight();
        AffineTransform at = new AffineTransform();
        at.rotate(Math.toRadians(90), x, y);
        Graphics2D g2 = (Graphics2D) b.getGraphics();
        g2.transform(at);
        g2.drawImage(img, 0, 0, null);
        return new ImageIcon(b);
    }
    
    public void resize(double pros) {
    /*    private Hashtable<String, ImgCharacter> character;
    private Hashtable<String, ImgFire> fire;
    private Hashtable<String, ArrayList<ImageIcon>> bomb;
    private Hashtable<String, ArrayList<ImageIcon>> brick;
    private Hashtable<String, ImageIcon> box;
    private Hashtable<String, ImageIcon> powerup;
    private ImageIcon welcome;
    
    */
        Iterator<ImgCharacter> ci = character.values().iterator();
        while (ci.hasNext()) {
            ImgCharacter c = ci.next();
            for (int i = 0; i < c.getDown().size(); i++) {
                c.getDown().get(i).setImage(resize(c.getDown().get(i).getImage(), pros));            
            }
            for (int i = 0; i < c.getLeft().size(); i++) {
                c.getLeft().get(i).setImage(resize(c.getLeft().get(i).getImage(), pros));            
            }
            for (int i = 0; i < c.getRight().size(); i++) {
                c.getRight().get(i).setImage(resize(c.getRight().get(i).getImage(), pros));            
            }
            for (int i = 0; i < c.getUp().size(); i++) {
                c.getUp().get(i).setImage(resize(c.getUp().get(i).getImage(), pros));            
            }
        }
        
        Iterator<ImgFire> fi = fire.values().iterator();
        while (fi.hasNext()) {
            ImgFire f = fi.next();
            for (int i = 0; i < f.getCore().size(); i++) {
                f.getCore().get(i).setImage(resize(f.getCore().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getDown().size(); i++) {
                f.getDown().get(i).setImage(resize(f.getDown().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getLeft().size(); i++) {
                f.getLeft().get(i).setImage(resize(f.getLeft().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getRight().size(); i++) {
                f.getLeft().get(i).setImage(resize(f.getLeft().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getRight().size(); i++) {
                f.getRight().get(i).setImage(resize(f.getRight().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getSide1().size(); i++) {
                f.getSide1().get(i).setImage(resize(f.getSide1().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getSide2().size(); i++) {
                f.getSide2().get(i).setImage(resize(f.getSide2().get(i).getImage(), pros));
            }
            for (int i = 0; i < f.getUp().size(); i++) {
                f.getUp().get(i).setImage(resize(f.getUp().get(i).getImage(), pros));
            }
        }
        
        Iterator<ArrayList<ImageIcon>> bi = bomb.values().iterator();
        while (bi.hasNext()) {
            ArrayList<ImageIcon> b = bi.next();
            for (int i = 0; i < b.size(); i++) {
                b.get(i).setImage(resize(b.get(i).getImage(), pros));
            }
        }
        
        Iterator<ArrayList<ImageIcon>> bri = brick.values().iterator();
        while (bri.hasNext()) {
            ArrayList<ImageIcon> b = bri.next();
            for (int i = 0; i < b.size(); i++) {
                b.get(i).setImage(resize(b.get(i).getImage(), pros));
            }
        }
        
        Iterator<ImageIcon> boxi = box.values().iterator();
        while (boxi.hasNext()) {
            ImageIcon i = boxi.next();
            i.setImage(resize(i.getImage(), pros));
        }
        
        Iterator<ImageIcon> pi = powerup.values().iterator();
        while (pi.hasNext()) {
            ImageIcon i = pi.next();
            i.setImage(resize(i.getImage(), pros));
        }
    }
    
    private Image resize(Image img, double pros) {
        BufferedImage b = new BufferedImage((int)((img.getWidth(null) / 100.0) * pros), (int)((img.getHeight(null) / 100.0) * pros), BufferedImage.TYPE_4BYTE_ABGR);
        b.getGraphics().drawImage(img, 0, 0, b.getWidth(), b.getHeight(), null);
        return b;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    } 
}
